<?php
/**
 * 队列测试脚本
 * 访问：http://你的域名/test_queue.php?user_id=1
 */

// 加载框架引导文件
require __DIR__ . '/../thinkphp/base.php';

// 获取用户ID参数
$userId = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;

if (!$userId) {
    echo json_encode([
        'code' => 0,
        'msg' => '请提供user_id参数，例如：test_queue.php?user_id=1'
    ], JSON_UNESCAPED_UNICODE);
    exit;
}

try {
    // 检查用户是否存在
    $user = \think\Db::name('identity_info')
        ->where(['user_id' => $userId, 'identity_type' => '教师'])
        ->find();
    
    if (!$user) {
        echo json_encode([
            'code' => 0,
            'msg' => "用户ID {$userId} 不存在或不是教师身份"
        ], JSON_UNESCAPED_UNICODE);
        exit;
    }
    
    // 推送队列任务
    $result = \think\Queue::push('app\\job\\GenerateTeacherWordJob', ['user_id' => $userId], 'default');
    
    if ($result) {
        echo json_encode([
            'code' => 1,
            'msg' => '队列任务推送成功',
            'data' => [
                'user_id' => $userId,
                'user_name' => $user['name'] ?? '',
                'queue_id' => $result,
                'tips' => '请查看队列监听窗口的日志输出，或查看 runtime/log/ 目录下的日志文件'
            ]
        ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    } else {
        echo json_encode([
            'code' => 0,
            'msg' => '队列任务推送失败，请检查Redis是否正常运行'
        ], JSON_UNESCAPED_UNICODE);
    }
    
} catch (\Exception $e) {
    echo json_encode([
        'code' => 0,
        'msg' => '异常：' . $e->getMessage(),
        'trace' => $e->getTraceAsString()
    ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}

